package com.icloud.wallet.boot;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.keywords.MySqlKeyWordsHandler;

import java.util.Collections;

public class MyBatisPlusGenerator {
    private static final String DB_NAME = "hello";
    // 数据库用户名
    private static final String USERNAME = "totem";
    private static final IDbQuery dbQuery = new MySqlQuery();
    // 数据库密码
    private static final String PASSWORD = "totem123";

    // 数据库连接字符
    private static final String URL = "jdbc:mysql://localhost:3306/" + DB_NAME + "?useUnicode=true&serverTimezone=UTC&useSSL=false&characterEncoding=utf8&tinyInt1isBit=false";
    // 数据库类型 MySQL

    // 项目根路径（注意：本项目中追加的是此组件模块的文件名。实际的单模块项目中，请使用下面的）
    private static final String projectPath = System.getProperty("user.dir") + "/icloud-wallet/icloud-wallet-boot" ;


    public static void main(String[] args) {
        System.out.println(projectPath);
        // 1.数据库配置
        DataSourceConfig.Builder dataSourceConfigBuilder = new DataSourceConfig.Builder(URL, USERNAME,
                PASSWORD).dbQuery(dbQuery).typeConvert(new MySqlTypeConvert() {
                    @Override
                    public IColumnType processTypeConvert(GlobalConfig config, String fieldType) {
                        //tinyint转成int
                        if (fieldType.toLowerCase().contains("tinyint")) {
                            return DbColumnType.INTEGER;
                        }
                        return super.processTypeConvert(config, fieldType);
                    }
                })
                .keyWordsHandler(new MySqlKeyWordsHandler());
        // 包路径
        String packagePath = projectPath + "/src/main/java";
        // XML文件的路径
        String mapperXmlPath = projectPath + "/src/main/resources";
        String packageName = MyBatisPlusGenerator.class.getPackage().getName();
        FastAutoGenerator.create(dataSourceConfigBuilder)
                .globalConfig(builder -> {
                    builder.author("raymond") // 设置作者
                            .commentDate("0000-00-00")
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .disableOpenDir() //生成完了禁止打开目录
                            .outputDir(packagePath); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent(packageName) // 设置父包名
                            .entity("model")
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperXmlPath + "/mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .logicDeleteColumnName("is_delete")
                            .enableLombok()
                            .addTableFills(new Column("created_at", FieldFill.INSERT))
                            .addTableFills(new Column("updated_at", FieldFill.INSERT_UPDATE))
                            .enableTableFieldAnnotation();
                    builder.addExclude("DATABASECHANGELOG", "DATABASECHANGELOGLOCK", "undo_log");
                })
                .templateConfig(builder -> builder.disable(TemplateType.CONTROLLER, TemplateType.SERVICE, TemplateType.SERVICEIMPL))
                .execute();
    }
}
